﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Utils {

    /// <summary>
    /// 本地数据保存工具类 -- 单例模式
    /// </summary>
    public class SettingsManager {

        // 私有的静态字段，用于保存单例实例  
        private static SettingsManager _instance;

        // 私有的构造函数，防止外部直接实例化  
        private SettingsManager() {}

        // 公开的静态方法，用于获取单例实例  
        public static SettingsManager Instance {
            get {
                if (_instance == null) {
                    lock (typeof(SettingsManager)) {
                        if (_instance == null) {
                            _instance = new SettingsManager();
                        }
                    }
                }
                return _instance;
            }
        }

        private string SettingsFilePath = Environment.CurrentDirectory + "\\settings.ini"; // INI文件路径  

        /// <summary>
        /// 保存数据至 本地文件
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void SaveSetting(string key, string value) {
            // 确保目录存在  
            Directory.CreateDirectory(Path.GetDirectoryName(SettingsFilePath));

            // 使用StringBuilder构建INI文件内容  
            StringBuilder sb = new StringBuilder();
            try {
                // 如果INI文件已存在，则读取其内容  
                if (File.Exists(SettingsFilePath)) {
                    string[] lines = File.ReadAllLines(SettingsFilePath);
                    foreach (string line in lines) {
                        // 如果不是当前要设置的键，则保留原内容  
                        if (!line.StartsWith(key + "=")) {
                            sb.AppendLine(line);
                        }
                    }
                }

                // 添加或更新设置项  
                sb.AppendLine(key + "=" + value);

                // 将内容写回INI文件  
                File.WriteAllText(SettingsFilePath, sb.ToString(), Encoding.UTF8);
            } catch (Exception ex) {
                Console.WriteLine("Error saving settings: " + ex.Message);
            }
        }

        /// <summary>
        /// 从本地文件中读取数据
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>值</returns>
        public string GetSetting(string key) {
            string value = string.Empty;
            if (File.Exists(SettingsFilePath)) {
                using (StreamReader sr = new StreamReader(SettingsFilePath)) {
                    string line;
                    while ((line = sr.ReadLine()) != null) {
                        // 分割键值对  
                        string[] parts = line.Split(new[] { '=' }, 2);
                        if (parts.Length == 2) {
                            string currentKey = parts[0].Trim();
                            if (string.Equals(currentKey, key, StringComparison.OrdinalIgnoreCase)) {
                                value = parts[1].Trim();
                                break; // 找到匹配项后退出循环  
                            }
                        }
                    }
                }
            } else {
                Console.WriteLine("Settings file not found: " + SettingsFilePath);
            }

            return value;
        }
    }
}
